package com.goldplusgold.tradedelay.app.home.live.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.goldplusgold.tradedelay.app.R;
import com.goldplusgold.tradedelay.app.home.live.adapter.ReTextLiveAdapter;
import com.goldplusgold.tradedelay.app.home.live.adapter.TextLiveAdapter;

public class TextLiveFragment extends Fragment {

    private int liveStatus ;
    private RecyclerView mRecyclerView;
    private String mRoomID;
    private LinearLayoutManager mLinearLayoutManager;
    private ReTextLiveAdapter mRecyclerViewAdapter;
    private int mLastVisibleItem = -1;
    public TextLiveFragment(String roomID , int liveStatus){
        this.mRoomID = roomID;
        this.liveStatus = liveStatus ;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRecyclerView =
                (RecyclerView) inflater.inflate(R.layout.list_fragment, container, false);
        return mRecyclerView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mLinearLayoutManager = new LinearLayoutManager(mRecyclerView.getContext());
        mRecyclerViewAdapter = new ReTextLiveAdapter(this.getActivity(),mRoomID , liveStatus);
        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        mRecyclerView.setAdapter(mRecyclerViewAdapter);
        mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView,
                                             int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE
                        && mLastVisibleItem + 1 == mRecyclerViewAdapter.getItemCount()) {
                    mRecyclerViewAdapter.pullUp();
               }

            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                mLastVisibleItem = mLinearLayoutManager.findLastVisibleItemPosition();
            }

        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mRecyclerViewAdapter.onChannleCancel();
        mRecyclerViewAdapter = null ;
    }
}
